package HashMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

public class HomeWork08 {
	public static void main(String[] args) {
		//准备牌
		ArrayList<String> list = new ArrayList<>();
		Collections.addAll(list, "0-大王","1-小王","2-♥2","3-♣2","4-♦2","5-♠2","6-♥A","7-♣A","8-♦A","9-♠A",
		        "10-♥K","11-♣K","12-♦K","13-♠K","14-♥Q","15-♣Q","16-♦Q","17-♠Q","18-♥J","19-♣J","20-♦J","21-♠J","22-♥10","23-♣10","24-♦10","25-♠10","26-♥9","27-♣9","28-♦9","29-♠9",
		        "30-♥8","31-♣8","32-♦8","33-♠8","34-♥7","35-♣7","36-♦7","37-♠7","38-♥6","39-♣6","40-♦6","41-♠6","42-♥5","43-♣5","44-♦5","45-♠5","46-♥4","47-♣4","48-♦4","49-♠4",
		        "50-♥3","51-♣3","52-♦3","53-♠3");

		HashMap<Integer, String> map = list.stream()
		        .map(s -> s.split("-"))
		        .filter(spl -> spl.length == 2)
		        .collect(Collectors.toMap(
		                spl -> Integer.parseInt(spl[0]),
		                spl -> spl[1],
		                (a, b) -> b,
		                HashMap::new
		        ));

		LinkedHashMap<Integer, String> shuffleMap = map.entrySet().stream()
		        .collect(Collectors.collectingAndThen(
		                Collectors.toList(),
		                shuffled -> {
		                    Collections.shuffle(shuffled);
		                    LinkedHashMap<Integer, String> result = new LinkedHashMap<>();
		                    shuffled.forEach(entry -> result.put(entry.getKey(), entry.getValue()));
		                    return result;
		                }
		        ));

		Player playe1 = new Player("老王");
		Player playe2 = new Player("老李");
		Player playe3 = new Player("老刘");

		int j = 0;
		for (int i : shuffleMap.keySet()) {
		    j++;
		    if (j <= 51) {
		        if (j % 3 == 0) {
		            playe1.getList().add(shuffleMap.get(i));
		        } else if (j % 3 == 1) {
		            playe2.getList().add(shuffleMap.get(i));
		        } else {
		            playe3.getList().add(shuffleMap.get(i));
		        }
		    }
		}

		LinkedHashMap<Integer, String> sortedPlayer1 = new LinkedHashMap<>();
		LinkedHashMap<Integer, String> sortedPlayer2 = new LinkedHashMap<>();
		LinkedHashMap<Integer, String> sortedPlayer3 = new LinkedHashMap<>();

		List<Integer> sortedKeys = new ArrayList<>(shuffleMap.keySet());
		Collections.sort(sortedKeys);

		sortedKeys.forEach(key -> {
		    String card = shuffleMap.get(key);
		    if (playe1.getList().contains(card)) {
		        sortedPlayer1.put(key, card);
		    } else if (playe2.getList().contains(card)) {
		        sortedPlayer2.put(key, card);
		    } else if (playe3.getList().contains(card)) {
		        sortedPlayer3.put(key, card);
		    }
		});

		System.out.println(playe1.getName() + " : " + sortedPlayer1.values());
		System.out.println(playe2.getName() + " : " + sortedPlayer2.values());
		System.out.println(playe3.getName() + " : " + sortedPlayer3.values());


	}
}
